package cn.ibizlab.odoo.core.odoo_calendar.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Icalendar_alarm;
import cn.ibizlab.odoo.core.odoo_calendar.domain.Calendar_alarm;
import cn.ibizlab.odoo.core.odoo_calendar.filter.Calendar_alarmSearchContext;
import cn.ibizlab.odoo.core.odoo_calendar.service.ICalendar_alarmService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[活动提醒] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Calendar_alarmServiceImpl implements ICalendar_alarmService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Icalendar_alarmClientService calendar_alarmClientService;

    @Override
    public Calendar_alarm get(Integer id) {
        Icalendar_alarm clientModel = calendar_alarmClientService.createModel();
        clientModel.setId(id);
        calendar_alarmClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Calendar_alarm get(Calendar_alarm calendar_alarm) {
        Calendar_alarm retcalendar_alarm = this.get(calendar_alarm.getId()) ;
    	BeanCopier copier=BeanCopier.create(retcalendar_alarm.getClass(), calendar_alarm.getClass(), false);
        copier.copy(retcalendar_alarm, calendar_alarm, null);
        return calendar_alarm ;
    }

    @Override
    public void createBatch(List<Calendar_alarm> calendar_alarms, int batchSize) {

                        //calendar_alarmClientService.createBatch(calendar_alarms,batchSize) ;

    }

    @Override
    public void updateBatch(List<Calendar_alarm> calendar_alarms, int batchSize) {

        //calendar_alarmClientService.updateBatch(calendar_alarms,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Icalendar_alarm clientModel = calendar_alarmClientService.createModel();
        clientModel.setId(id);
        calendar_alarmClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(Calendar_alarm calendar_alarm) {
        boolean bOk = false ;
        Icalendar_alarm clientModel = this.convert2Model(calendar_alarm,null);
        calendar_alarmClientService.create(clientModel) ;
        this.convert2Domain(clientModel,calendar_alarm);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Calendar_alarm calendar_alarm) {
        boolean bOk = false ;
        Icalendar_alarm clientModel = this.convert2Model(calendar_alarm,null);
        calendar_alarmClientService.update(clientModel) ;
        this.convert2Domain(clientModel,calendar_alarm) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //calendar_alarmClientService.removeBatch(calendar_alarms) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Calendar_alarm> searchDefault(Calendar_alarmSearchContext context) {
        List<Calendar_alarm> list = new ArrayList<Calendar_alarm>();
        Page<cn.ibizlab.odoo.core.client.model.Icalendar_alarm> clientModelList = calendar_alarmClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icalendar_alarm clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Calendar_alarm>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Calendar_alarm> selectByOdoo_create(Integer id){
        Calendar_alarmSearchContext context = new Calendar_alarmSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Calendar_alarm> list = new ArrayList<Calendar_alarm>();
        Page<cn.ibizlab.odoo.core.client.model.Icalendar_alarm> clientModelList = calendar_alarmClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icalendar_alarm clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Calendar_alarm>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Calendar_alarm> selectByOdoo_write(Integer id){
        Calendar_alarmSearchContext context = new Calendar_alarmSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Calendar_alarm> list = new ArrayList<Calendar_alarm>();
        Page<cn.ibizlab.odoo.core.client.model.Icalendar_alarm> clientModelList = calendar_alarmClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Icalendar_alarm clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Calendar_alarm>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Icalendar_alarm convert2Model(Calendar_alarm domain , Icalendar_alarm model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = calendar_alarmClientService.createModel() ;
        }

         if(domain.getIntervalDirtyFlag())
            model.setInterval(domain.getInterval());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getDurationDirtyFlag())
            model.setDuration(domain.getDuration());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getTypeDirtyFlag())
            model.setType(domain.getType());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getDuration_minutesDirtyFlag())
            model.setDuration_minutes(domain.getDuration_minutes());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Calendar_alarm convert2Domain( Icalendar_alarm model ,Calendar_alarm domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Calendar_alarm();
        }

        if(model.getIntervalDirtyFlag())
            domain.setInterval(model.getInterval());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getDurationDirtyFlag())
            domain.setDuration(model.getDuration());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getTypeDirtyFlag())
            domain.setType(model.getType());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getDuration_minutesDirtyFlag())
            domain.setDuration_minutes(model.getDuration_minutes());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
